package com.chaoxing.service;


import com.chaoxing.convert.BookConvert;
import com.chaoxing.bean.DO.Book;
import com.chaoxing.bean.DTO.Result;
import com.chaoxing.bean.DTO.ResultTable;
import com.chaoxing.bean.VO.BookVO;
import com.chaoxing.bean.VO.PageVO;
import com.chaoxing.mapper.BookMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService {


    @Autowired
    private BookMapper bookMapper;


    public String insertBook(BookVO bookVO) {


        Book book = BookConvert.INSTANCE.bookVOToBookDO(bookVO);

        bookMapper.insert(book);

        return "新增成功";

    }

    public ResultTable bookList(PageVO pageVO) {

        PageHelper.startPage(pageVO.getPageNum(), pageVO.getPageSize());

        PageInfo pageInfo = new PageInfo(bookMapper.selectAll());

        ResultTable resultTable = new ResultTable() ;

        resultTable.setCode(0);
        resultTable.setData(pageInfo.getList());
        resultTable.setMassage("查询成功");
        resultTable.setCount(pageInfo.getTotal());


        return resultTable;

    }

    public Book getBookDetail(String id) {
        Book book = new Book() ;
        book.setId(Long.parseLong(id));
        return bookMapper.selectOne(book) ;
    }

    public Result deleteBook(String id) {
        Book book = new Book() ;
        book.setId(Long.parseLong(id));
        bookMapper.delete(book) ;
        return Result.success("删除成功") ;
    }
}
